<?php
namespace app\admin\controller;

class Logtest extends Base
{
    //列表
    public function lists(){
        if(is_post()){
            $d = $this->post;
            $map = [];
            if($d['keyword']!=''){ $map[] = ['and','uuid','like',"%{$d['keyword']}%"]; }

            $w  = where($map);
            $rs = $this->db->query("select * from log_test $w[0] order by id desc".limit($d['page'],$d['limit']), $w[1],'',true);
            if($rs){
                $rule = [
                    'uuid' => function($v,$val){
                        $html = "<a target='_blank' href=".url('logtest/detail')."'?ids[]={$val['id']}'>{$val['uuid']}</a>";
                        return $html;
                    },
                    'create_time' => 'com_time_format',
                ];
                $data = com_format($rs[0],$rule);
                return suc($data, $rs[1]);
            }else{
                suc([],0);
            }
            $this->fetch();
        }
        return $this->fetch();
    }

    public function detail(){
        $ids = $this->get['ids'];
        foreach ($ids as $key => $val) {
            $ids[$key] = (int)$val;
        }
        $ids = implode(',',$ids);
        $rs = $this->db->query("select * from log_test where id in(".$ids.") order by id desc");
        $html = "";
        foreach ($rs as $val) {

            $html .= "uuid:".$val['uuid'].'<br/>';
            $html .= "时间:".com_time_format($val['create_time'],'f').'<br/>';
            $html .= "来路:".$val['url'].'<br/>';
            $text = json_decode($val['content'],true);
            $content = '';
            if (is_array($text)) {
                foreach ($text as $k=>$v) {
                    $content .= '<span style="display:inline-block;width:150px;">'.$k.'</span>：'.$v.'<br/>';
                }

            }
            $html .= "内容:<br/>".$content.'<br/>';
            $html .= '<hr/>';
        }

        echo $html;

    }

    #删除
    public function del(){
      if(is_post()){
        $id = intval($this->post["id"]);
        $rs = $this->db->delete("log_test",$id);
        if($rs){
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
        }else{
            return err("删除失败");
        }
      }
    }

    #批量删除
    function delall(){
      if(is_post()){
            $d = $this->post;
            $ids = $d['id'];
            foreach($ids as $id){
                $this->db->delete('log_test',$id);
            }
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
      }
    }
}
